package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.QueryPageParam;
import com.example.common.Result;
import com.example.pojo.Title;
import com.example.pojo.User;
import com.example.service.TitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 题目登记表 前端控制器
 * </p>
 *
 * @author zzw
 * @since 2023-10-29
 */
@RestController
@RequestMapping("/title")
public class TitleController {
    @Autowired
    private TitleService titleService;
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {

        HashMap param = query.getParam();
        String number = (String)param.get("number");
        String name = (String)param.get("name");
        String instructor = (String)param.get("instructor");
        String dept = (String)param.get("dept");
        String phone = (String)param.get("phone");

        Page<Title> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Title> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(number) && !"null".equals(number)) {
            lambdaQueryWrapper.like(Title::getNumber, number);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Title::getName, name);
        }
        if(StringUtils.isNotBlank(instructor) && !"null".equals(instructor)) {
            lambdaQueryWrapper.like(Title::getInstructor, instructor);
        }
        if(StringUtils.isNotBlank(dept) && !"null".equals(dept)) {
            lambdaQueryWrapper.like(Title::getDept, dept);
        }
        if(StringUtils.isNotBlank(phone) && !"null".equals(phone)) {
            lambdaQueryWrapper.like(Title::getPhone, phone);
        }
        IPage result = titleService.page(page, lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/findByNumber")
    public Result findByNo(@RequestParam String number) {
        List<Title> list = titleService.lambdaQuery().eq(Title::getNumber, number).list();
        return list.size() > 0 ? Result.success(list) : Result.fall();
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {
        return titleService.removeById(id) ? Result.success() : Result.fall();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Title title) {
        return titleService.save(title) ? Result.success() : Result.fall();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Title title) {
        return titleService.updateById(title) ? Result.success() : Result.fall();
    }
}
